import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
import Controls from '#cpt/temp/Controls'
import { patientFormData } from '../../common/defaultData'
// ===================================================================== global declare
const { $fn, $Lazy, $http, globalConfig } = window
const { COVIDUrl } = globalConfig
// ===================================================================== Lazy
const Box 			= $Lazy.load(()=>import('#tp/box/box'))
const $cache 		= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:{},
		detailData:{},
		isEdit:false,	// 控制表单是否可编辑
		apiType: '',	// 编辑、添加、审核接口？
		formData: patientFormData.call(this)
	}
	submit = {}
	componentDidMount(){
		this.getSelect()
	}
	
	setValue = (uuid) => {		
		const { formData } = this.state
		$fn.disabledRows(this.state.formData, ['specCode', 'hospId' ], false)
		$http.submit(null,COVIDUrl+'caseinfo/getrow',{ param:{uuid}, loading:false }).then(data=>{
			this.formRef.reset()
			for(let key in data){
				if($fn.isString(data[key])){
					data[key]=data[key].replace(/(^\s*)|(\s*$)/g,"")
				}
			}
			if(+data.checkType === 1){
				$fn.getRow(formData, 'cardNo').required = false
			}else{
				$fn.getRow(formData, 'cardNo').required = true
			}
			this.formRef.setValue(data)
			this.setState({ formData, detailData:data,isEdit:false})
		})
	}
	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		
		$cache.then( async f => {
			// 医院名称
			$fn.setRow( formData, 'hospId', await f.getCache({ api:COVIDUrl+'Hospital/query',param:{page:1,page_num:10000,status:1} }) )

			this.setState({ formData })
		})
	}
	
	// 病人详细信息按钮
	controls = () => {
		const {isEdit,formData} = this.state		
		// 保存 查看图片 审核 编辑
		return [
			Controls.click.call(this, '保存', ()=>{
				this.formRef.submit()
			},{hide:!isEdit, code: 'F2'} ),
			Controls.click.call(this, '编辑', ()=>{											// 编辑
				if(!$fn.hasObject(this.state.detailData)){
					$fn.msg.warning('请先选择要编辑的信息')
				}else{
					const {uuid} = this.state.detailData
					this.submit.uuid = uuid
					this.setState({isEdit: true, apiType:'edit' })
				}
			},{hide:isEdit, code: 'F1'} ),	
			Controls.click.call(this, '添加', ()=>{										// 添加
				this.formRef.reset()
				$fn.getRow(formData, 'cardNo').required = true
				this.formInit()
				delete this.submit.uuid
				this.setState({isEdit: true, apiType:'add' })
			},{hide:isEdit, code: 'F3'} ),
			Controls.click.call(this, '返回', ()=>{
				this.formRef.setValue(this.state.detailData)
				this.setState({isEdit: false })
			},{hide:!isEdit} )
		]
	}
	
	formInit = () => {
		this.formRef.setValue({sex:1, specTraits:'2', checkType:0, cardType:6, caseType:14 })
	}
	
	render(){
		const { isEdit, apiType, submitLoading } = this.state
		return (
			<>
				<Box title='病人详细信息' small full style={{height:'100%'}} contentClassName='mt10' scroll flex loading={submitLoading} controls={this.controls()}>
					{
						SubmitForm.call(this,{ disabled:!isEdit, labelWidth:'s7', noholder:true,
							api:apiType ==='add'?COVIDUrl+'caseinfo/create':apiType ==='edit'?COVIDUrl+'caseinfo/update':null, 
							onRefresh:()=>{
								this.props.onRefresh()
								this.setState({isEdit:false})
								if(apiType ==='add'){
									this.formRef.reset()
									this.formInit()
								}
							}
						} ) 
					}
				</Box>
			</>
		)
	}
}

export default Index